package io.imp.modules.tasks.unionqrc.service;

import io.imp.common.utils.AesEncryptUtils;
import io.imp.modules.sys.service.SysDictService;
import io.imp.modules.tasks.unionqrc.entity.ItcsTerminalInfo;
import io.imp.modules.tasks.unionqrc.entity.ItcsTransOrderJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnlPart;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * 功能描述:
 * <支付交易文件数据处理>
 *
 * @ClassName: WorkItcsUnionpayReverseJnl
 * @Date: 2019年05月17日 13:31:04
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
public class WorkItcsUnionpayTxnJnl implements Callable {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService;

    private ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService;

    private ItcsTransOrderJnlService itcsTransOrderJnlService;

    private SysDictService sysDictService;

    private Map<String,ItcsTransOrderJnl> itcstransOrderJnlMap ;

    private Map<String,String> stationMap;

    private List<ItcsUnionpayTxnJnl> source;

    private CountDownLatch countDownLatch;

    private ItcsTerminalInfoService itcsTerminalInfoService;

    public WorkItcsUnionpayTxnJnl(ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService,
                                  ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService,
                                  ItcsTransOrderJnlService itcsTransOrderJnlService,
                                  SysDictService sysDictService, List<ItcsUnionpayTxnJnl> source,
                                  CountDownLatch countDownLatch,Map<String,ItcsTransOrderJnl> itcstransOrderJnlMap,
                                  Map<String,String> stationMap){
        this.itcsUnionpayTxnJnlService = itcsUnionpayTxnJnlService;
        this.itcsUnionpayTxnJnlPartService = itcsUnionpayTxnJnlPartService;
        this.itcsTransOrderJnlService = itcsTransOrderJnlService;
        this.sysDictService = sysDictService;
        this.source = source;
        this.countDownLatch = countDownLatch;
        this.itcstransOrderJnlMap = itcstransOrderJnlMap;
        this.stationMap = stationMap;
    }
    public WorkItcsUnionpayTxnJnl(ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService,
                                  ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService,
                                  ItcsTransOrderJnlService itcsTransOrderJnlService,
                                  SysDictService sysDictService, List<ItcsUnionpayTxnJnl> source,
                                  CountDownLatch countDownLatch,Map<String,ItcsTransOrderJnl> itcstransOrderJnlMap,
                                  Map<String,String> stationMap,ItcsTerminalInfoService itcsTerminalInfoService){
        this.itcsUnionpayTxnJnlService = itcsUnionpayTxnJnlService;
        this.itcsUnionpayTxnJnlPartService = itcsUnionpayTxnJnlPartService;
        this.itcsTransOrderJnlService = itcsTransOrderJnlService;
        this.sysDictService = sysDictService;
        this.source = source;
        this.countDownLatch = countDownLatch;
        this.itcstransOrderJnlMap = itcstransOrderJnlMap;
        this.stationMap = stationMap;
        this.itcsTerminalInfoService = itcsTerminalInfoService;
    }
    @Override
    public String call() throws Exception {
        logger.info("支付流水数据处理...");
        StringBuilder result = new StringBuilder();
        List<ItcsUnionpayTxnJnlPart> itcsUnionpayTxnJnlParts = new LinkedList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        Calendar calendar = Calendar.getInstance();
        if (source.size()!=0){
            for (ItcsUnionpayTxnJnl I:source) {

                String orderNo = I.getOrderid();
                // 判断空指针
                String estationid;
                String terminalNo;
                ItcsTransOrderJnl itcsTransOrderJnl = null;
                if (itcstransOrderJnlMap.get(orderNo) == null){
                    itcsTransOrderJnl = itcsTransOrderJnlService.getByOrderNo(orderNo);
                    estationid = itcsTransOrderJnl.getEstationid();
                    terminalNo = itcsTransOrderJnl.getTerminalno();
                }else {
                    itcsTransOrderJnl = itcstransOrderJnlMap.get(orderNo);
                    estationid = itcsTransOrderJnl.getEstationid();
                    terminalNo = itcsTransOrderJnl.getTerminalno();
                }
                if (StringUtils.isEmpty(estationid)){
                    estationid = "G0000000000000";
                    if(StringUtils.isNotEmpty(terminalNo)){
                        ItcsTerminalInfo terminalInfo = itcsTerminalInfoService.getItcsTerminalInfo(terminalNo);
                        if(terminalInfo!=null && StringUtils.isNotEmpty(terminalInfo.getStationId())){
                            estationid = terminalInfo.getStationId();
                        }
                    }
                }

                //获取站名
                String stationName = stationMap.get(estationid);

                String qrNo = I.getQrNo();
                logger.info("渠道订单的qrNo:{}",qrNo);
                //解密后的qrNo
                String afterQrNo = null;
                try {
                    afterQrNo = AesEncryptUtils.decrypt(qrNo,sysDictService.getDictValue("unionqrc","encryptKey"));
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.info("解密失败，异常信息为:{}",e.getMessage());
                }

                String txntype = I.getTxntype();
                txntype = StringUtils.isBlank(txntype)?"01":txntype;
                String txnsubtype = I.getTxnsubtype();
                txnsubtype = StringUtils.isBlank(txnsubtype)?"06":txnsubtype;
                result.append(I.getMerid()).append("|").append(I.getTermId()).append("|").append(stationName).append("|").append(I.getOrderid())
                        .append("|").append(I.getTxntime()).append("|").append(afterQrNo).append("|").append(I.getTxnamt()).append("|").append(txntype).append("|")
                        .append(txnsubtype).append("|").append(I.getTraceno()).append("|").append(I.getRespcode()).append("|").append(I.getRespmsg())
                        .append("|").append(I.getSettledate()).append("|").append(I.getSettleamt()).append("|").append(I.getAccno()).append("|").append("|").append("\r\n");
                itcsUnionpayTxnJnlService.update("1",I.getTxnno());

            }
        }else {
            result.append("");
        }

        this.countDownLatch.countDown();
        return result.toString().replace("null","");
    }
}
